package com.dzm.controller;

import com.dzm.entity.Advertisement;
import com.dzm.service.AdvertisementService;
import com.dzm.utils.PageResult;
import com.dzm.utils.R;
import com.dzm.vo.SearchVO;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 作者：Dzm
 * 时间：2024-08-10-9:38
 * 功能：
 */

@RestController
@RequestMapping("/advertisement")
public class AdvertisementController {

    @Autowired
    private AdvertisementService advertisementService;

    //条件带分页查询
    @PostMapping("/search/{pageNum}/{pageSize}")
    public R search(@PathVariable("pageNum") Integer pageNum,
                    @PathVariable("pageSize") Integer pageSize,
                    @RequestBody SearchVO searchVO) {

        Page<Advertisement> page = advertisementService.search(pageNum, pageSize, searchVO);
        PageResult<Advertisement> pageModel = new PageResult<>(page.getTotal(), page.getResult());

        return R.ok().data("pageModel", pageModel);

    }

    @GetMapping("/getAdvertisementById/{id}")
    public R getAdvertisement(@PathVariable("id") Integer id) {
        Advertisement advertisement = advertisementService.getById(id);

        return R.ok().data("adver", advertisement);
    }

    //添加广告
    @PostMapping("/add")
    public R add(@RequestBody Advertisement advertisement) {
        advertisementService.addAdvertisement(advertisement);

        return R.ok();
    }

    //修改广告
    @PutMapping("/update")
    public R update(@RequestBody Advertisement advertisement) {
        advertisementService.updateAdvertisement(advertisement);

        return R.ok();
    }

    //删除广告
    @DeleteMapping("/remove/{ids}")
    public R remove(@PathVariable String ids) {
        advertisementService.deleteByIds(ids);

        return R.ok();
    }


}
